﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SomaiNordeste.FabricaRacao.DomainModel.Repository;
using SomaiNordeste.FabricaRacao.DomainModel;

namespace SomaiNordeste.FabricaRacao.Presentation
{
    public partial class frmInsumoEdicao : Form
    {
        private InsumoRepository _insumoRepository;
        private Insumo _insumo;

        public frmInsumoEdicao()
        {
            InitializeComponent();
            _insumoRepository = new InsumoRepository();
            _insumo = new Insumo();
        }

        private void frmInsumoEdicao_Load(object sender, EventArgs e)
        {
            CarregaRacoes(date.Value);
        }

        private void CarregaRacoes(DateTime dateTime)
        {
            cmbRacao.DataSource = _insumoRepository.FindBy(dateTime);
            cmbRacao.DisplayMember = "Racao";
        }

        private void date_ValueChanged(object sender, EventArgs e)
        {
            CarregaRacoes(date.Value);
        }

        private void cmbRacao_SelectedIndexChanged(object sender, EventArgs e)
        {
            Insumo insumo = new Insumo();

            insumo = ((Insumo)cmbRacao.SelectedValue);
            if (cmbRacao.SelectedIndex > 0)
                grdRacoes.DataSource = _insumoRepository.FindBy(insumo, date.Value).DefaultView;
        }

        private void grdRacoes_Click(object sender, EventArgs e)
        {
            
            if (grdRacoes.CurrentRow == null || string.IsNullOrEmpty(
                grdRacoes.Rows[grdRacoes.CurrentRow.Index].Cells[0].Value.ToString()))
            {
                txtPeso.Text = string.Empty;
                lblRacao.Text = string.Empty;
                return;
            }

            _insumo = new Insumo();
            _insumo.Date = DateTime.Parse(grdRacoes.Rows[grdRacoes.CurrentRow.Index].Cells[0].Value.ToString());
            _insumo.Produto = grdRacoes.Rows[grdRacoes.CurrentRow.Index].Cells[1].Value.ToString();
            _insumo.Batida = int.Parse(grdRacoes.Rows[grdRacoes.CurrentRow.Index].Cells[2].Value.ToString());
            _insumo.Peso = double.Parse(grdRacoes.Rows[grdRacoes.CurrentRow.Index].Cells[3].Value.ToString());

            lblRacao.Visible = true;
            lblRacao.Text = _insumo.Produto;
            txtPeso.Text = _insumo.Peso.ToString();
        }

        private void btnAlterar_Click(object sender, EventArgs e)
        {
            _insumo.Peso_Atualizado =double.Parse(txtPeso.Text);
            _insumoRepository.Update(_insumo);
            MessageBox.Show("Peso alterado com sucesso!");


            Insumo batida = new Insumo();
            batida = ((Insumo)cmbRacao.SelectedValue);
            if (cmbRacao.SelectedIndex > 0)
                grdRacoes.DataSource = _insumoRepository.FindBy(batida, date.Value).DefaultView;
        }

        private void cmbRacao_Enter(object sender, EventArgs e)
        {
            cmbRacao.BackColor = Color.Yellow;
        }

        private void cmbRacao_Leave(object sender, EventArgs e)
        {
            cmbRacao.BackColor = Color.White;
        }

        private void txtPeso_Enter(object sender, EventArgs e)
        {
            txtPeso.BackColor = Color.Yellow;
        }

        private void txtPeso_Leave(object sender, EventArgs e)
        {
            txtPeso.BackColor = Color.White;
        }

        

    }
}
